#include "mixereditone.h"
#include "ui_mixereditone.h"

MixerEditOne::MixerEditOne(QWidget *parent, int sentenceID) :
    QDialog(parent),
    ui(new Ui::MixerEditOne)
{
    ui->setupUi(this);
    this->sentenceID = sentenceID;
    readDataFromDB();
    testGrouping();
    QRegExp rx("[1-9]*");
    groupingValidator = new QRegExpValidator(rx, this);
    rx.setPattern("^(?!\\s)((?!\\*)(?!\\s\\s).)*$");
    sentenceValidator = new QRegExpValidator(rx, this);
    ui->lineEdit_EnSentence->setValidator(sentenceValidator);
    ui->lineEdit_Grouping->setValidator(groupingValidator);
    connect(ui->lineEdit_EnSentence, SIGNAL(textChanged(QString)), this, SLOT(testGrouping()));
    connect(ui->lineEdit_Grouping, SIGNAL(textChanged(QString)), this, SLOT(testGrouping()));
}

MixerEditOne::~MixerEditOne()
{
    delete ui;
}

void MixerEditOne::readDataFromDB()
{
    model.setTable("mix_sentences");
    model.setRelation(1, QSqlRelation("mix_themes", "tid", "teng"));
    model.setFilter("sid=" + QString::number(sentenceID));
    model.select();
    QSqlTableModel *rel = model.relationModel(1);
    ui->comboBox_Theme->setModel(rel);
    ui->comboBox_Theme->setModelColumn(1);
    mapper.setModel(&model);
    mapper.setItemDelegate(new QSqlRelationalDelegate(this));
    mapper.addMapping(ui->lineEdit_SentenceID, 0);
    mapper.addMapping(ui->comboBox_Theme, 1);
    mapper.addMapping(ui->lineEdit_HuSentence, 4);
    mapper.addMapping(ui->lineEdit_EnSentence, 2);
    mapper.addMapping(ui->lineEdit_Grouping, 3);
    mapper.toFirst();
}

void MixerEditOne::writeDatatoDB()
{
    ui->lineEdit_EnSentence->setText(ui->lineEdit_EnSentence->text().trimmed());
    ui->lineEdit_HuSentence->setText(ui->lineEdit_HuSentence->text().trimmed());
    mapper.submit();
}

bool MixerEditOne::groupingPreview()
{
    QString instring(ui->lineEdit_EnSentence->text());
    QString grpstr(ui->lineEdit_Grouping->text());
    QString outstring;
    QStringList splited = instring.split(" ");
    int wordcnt = splited.count();
    int grpstringlength = grpstr.length();
    int pos = 0;
    if (wordcnt == 1 && instring.length() == 0)
        wordcnt = 0;
    for (int i = 0; i < grpstringlength; ++i) {
        int cwords = grpstr.mid(i, 1).toInt();
        outstring.append("<b>{</b>");
        for (int w = 0; w < cwords; ++w) {
            if (pos < wordcnt)
                outstring.append(splited.at(pos));
            else
                outstring.append("<b>+</b>");
            if (cwords > 1)
                outstring.append(" ");
            ++pos;
        }
        outstring.append("<b>}</b> ");
    }
    for (int i = pos; i < wordcnt; ++i) {
        outstring.append("<b>-</b> ");
    }
    ui->label_GrpPreview->setText(outstring);
    if (pos == 0 || pos == wordcnt)
        return true;
    else
        return false;
}

void MixerEditOne::testGrouping()
{
    bool ok = groupingPreview();
    if (ok)
        ui->label_GrpPreview->setText("<b>[OK]</b><br>" + ui->label_GrpPreview->text());
    else
        ui->label_GrpPreview->setText("<b>[FAIL]</b><br>" + ui->label_GrpPreview->text());
}
